home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Checking For Keyboard Input
- Date: 26 Feb 1996 14:07:00 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gtau4INNbtr@keats.ugrad.cs.ubc.ca>
- References: <cerebus.34.000DD98A@voicenet.com> <4gqriq$qf2@maureen.teleport.com> <3131EA31.167E@mozart.bme.ohio-state.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <3131EA31.167E@mozart.bme.ohio-state.edu>,
- Xiaoyi Wu <xiaoyi@mozart.bme.ohio-state.edu> wrote:
- >GHouck wrote:
- >>
- >> cerebus@voicenet.com (aLEX) wrote:
- >> >Hello fellow C programmers,
- >> > Is there anyway to check if keyboard input has been entered without
- >> >actually stalling the excution of the code until the user presses a key. I
- >> >would really like to do this but I can't seem to find any way. If you know of
- >> >a way PLEASE email me or post it. Thank you in advance.
- >> > aLEX
- >> > cerebus@voicenet.com
- >> aLEX,
- >> There is if you're using Borland on DOS/Windows: there is a function called
- >> kbhit()
- >> If a key has been hit, it returns a non-zero value, else it returns zero. If
- >> one is available you can then get it with another call.
- >> Yours, Geoff Houck
- >
- >What about Unix?
-
- You can write a UNIX kbhit() function, but it will require that some
- initialization and cleanup code be called. This is why the FAQ recommends that
- a properly written kbhit() will have a setup call and a cleanup call---on some
- systems, the terminal driver has to be configured for character-at-a-time mode.
-
- The FAQ discusses the many good reasons why such functionality was not drafted
- into the standards.
-
- In any case, a UNIX kbhit would just look like something like this, after
- appropriate setup of the terminal driver:
-
-
- #include <sys/time.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <fcntl.h>
-
- int kbhit(void)
-
- {
- fd_set fds; /* file descriptor set */
- struct timeval tv = { /* timer value structure */
- 0, 0 /* zero seconds, microseconds */
- };
-
- FD_ZERO(&fds); /* clear file descriptor set */
- FD_SET(0,&fds); /* add standard input to it */
-
- return select(1,&fds,NULL,NULL,&tv);
- }
-
- The select() function is used to test multiple descriptors for input, readiness
- to take output, or the presence of exception conditions. If you specify a
- zero-valued timeout structure, it effectuates a quick poll.
-
- This code assumes that your terminal input is file descriptor 0, for clarity.
-
- There are other ways to accomplish this. You can use a non-blocking read on the
- terminal. Or you can set up a special mode on the terminal that will allow you
- to check for characters. However, both these other methods will dequeue the
- character if it is available. select() will not cause any I/O to take place.
-
-
- The way you set up a terminal so that you can do the above for each keystroke
- is:
-
- #include <termio.h>
-
- static struct termios save;
-
- void rawmode(void) /* set up raw behavior on terminal */
- {
- struct termios change;
-
- tcgetattr(0, &change); /* get attributes of terminal device */
- /* assumes fd 0 is a terminal :( bad! */
-
- save = change; /* save previous settings */
-
- /*
- * code to change the settings in the ``change structure''
- */
-
- tcsetattr(0, TCSAFLUSH, &change);
- }
-
- void restore(void) /* restore terminal */
- {
- tcsetattr(0, TCSAFLUSH, &save);
- }
-
-
- The ``code to change the settings'' is probably found in the FAQ. I don't want
- to get into it. Get a book!
- --
-
-